/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.store.client.components;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.elf.gwt.common.client.model.ManufacturerMetaModel;
import com.elf.gwt.common.client.model.ManufacturerModel;
import com.elf.gwt.store.client.StoreComponentController;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;

/**
 *  
 */
public class ManufacturersComponent extends JSONServerComponent
{	
	protected ManufacturerMetaModel manufacturerMetaModel;
	protected ManufacturerModel manufacturerModel;
	protected List<ManufacturerModel> manufacturerModels;

	protected Grid bodyPanel;
	protected ListBox listBox;

	/**
	 * Constructor
	 */
	public ManufacturersComponent(StoreComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix, null, false);

		manufacturerModel = new ManufacturerModel();
		manufacturerModels = new ArrayList<ManufacturerModel>();

		bodyPanel = new Grid(1,1);
		bodyPanel.addStyleName("ComponentBodyPanel");
		bodyPanel.addStyleName(getHandle() + "BodyPanel");

		listBox = new ListBox();
		listBox.addChangeHandler(changeHandler);
		bodyPanel.setWidget(0, 0, listBox);

		CellFormatter cf = bodyPanel.getCellFormatter();
		cf.setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
	}

	public boolean updateFromHistoryParameters(String[] params)
	{
		int id = 0;
		if (params.length > 1) id = Integer.parseInt(params[1]);
		int sz = manufacturerModels.size();
		for (int i = 0; i < sz; i++)
		{
			ManufacturerModel model = manufacturerModels.get(i);
			if (model.getId() == id)
			{
				manufacturerModel.update(model);
				listBox.setSelectedIndex(i);
			}
		}

		if (StoreComponentController.productComponent != null) StoreComponentController.productComponent.getProductModel().update(0, 0, "TBD");
		StoreComponentController.categoriesComponent.getCategoryModel().update(StoreComponentController.categoriesComponent.getCategoryModel(0));
		storeController.getMultipleResultsComponent().addComponent(StoreComponentController.categoriesComponent);
		
		return super.updateFromHistoryParameters(params);
	}

	public void onBuilt()
	{
		int sz = manufacturerModels.size();
		setVisible(sz > 1);
	}
	
	public void build(JSONValue jsonValue)
	throws Exception
	{
		super.build(jsonValue);
		if (manufacturerModels.size() == 0)
		{
			ManufacturerModel model = new ManufacturerModel();
			manufacturerModels.add(model);
		}
	}
	protected void buildMetaModel(JSONObject jsonObject)
	{
		manufacturerMetaModel = new ManufacturerMetaModel(jsonObject);
	}

	protected void buildModel(JSONArray jsonArray) throws Exception
	{
		manufacturerModels.clear();
		listBox.clear();
		super.buildModel(jsonArray);
	}

	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		ManufacturerModel model = new ManufacturerModel(jsonObject);
		model.setIndex(manufacturerModels.size());
		manufacturerModels.add(model);
	}

	public void buildFromModel() throws Exception
	{
		int i = 0;
		listBox.clear();
		Iterator<ManufacturerModel> iter = manufacturerModels.iterator();
		while(iter.hasNext())
		{
			ManufacturerModel model = iter.next();
			listBox.addItem(model.getName(), String.valueOf(model.getId()));
			if (model.getId() == manufacturerModel.getId())
			{
				listBox.setSelectedIndex(i);
			}
			i++;
		}
		panel.setWidget(1, 0, bodyPanel);
		onBuilt();
	}

	protected ChangeHandler changeHandler = new ChangeHandler() {
		public void onChange(ChangeEvent event) {
			ManufacturerModel model = getManufacturerModel(listBox.getSelectedIndex());
			onSelection("ManufacturersComponent--" + model.getId());
		}
	};

	public ManufacturerModel getManufacturerModel(int index) {
		return manufacturerModels.get(index);
	}

	public ManufacturerModel getManufacturerModelById(int id) {
		ManufacturerModel manufacturerModel = null;
		for (Iterator<ManufacturerModel> iter = manufacturerModels.iterator(); iter.hasNext(); )
		{
			ManufacturerModel model = iter.next();
			if (model.getId() == id) {
				manufacturerModel = model;
				break;
			}
		}
		return manufacturerModel;
	}

	public List<ManufacturerModel> getManufacturerModels() {
		return manufacturerModels;
	}

	public ManufacturerModel getManufacturerModel() {
		return manufacturerModel;
	}
}
